package com.qbw.core.okhttp;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.http.okhttp.HttpTask;
import com.lib.util.DateTimeUtil;
import com.lib.util.DeviceUtil;
import com.qbw.app.BuildConfig;
import com.qbw.app.manager.UserManager;
import com.qbw.app.model.User;
import com.qbw.app.module.common.util.CountryUtil;


import java.util.HashMap;
import java.util.Map;

/**
 * @author qinbaowei
 * @createtime 2017/04/10 11:36
 * @email qbaowei@qq.com
 * @description
 */


public class HttpParam implements HttpTask.ICommonHeadersAndParameters {

    private String IMEI;
    private String IMSI;
    private String VERSIONCODE;
    private String VERSIONNAME;
    private String CHANNELID;

    private static HttpParam sInst;

    private HttpParam() {}

    public static HttpParam getInstance() {
        if (sInst == null) {
            synchronized (HttpParam.class) {
                if (sInst == null) {
                    sInst = new HttpParam();
                }
            }
        }
        return sInst;
    }

    @Override
    public void init(Context context) {
        IMEI = DeviceUtil.getIMEI(context);
        IMSI = DeviceUtil.getIMSI(context);
        VERSIONCODE = BuildConfig.VERSION_CODE + "";
        VERSIONNAME = BuildConfig.VERSION_NAME;
        CHANNELID = BuildConfig.FLAVOR;
    }

    @Override
    public Map<String, String> getHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put("deviceId", IMEI);
        headers.put("imsi", IMSI);

        headers.put("channel", CHANNELID);
        headers.put("device", android.os.Build.MODEL);
        headers.put("platform", "android");
        headers.put("version", "1");
        headers.put("languageId", CountryUtil.getLanguageId() + "");
        User user = UserManager.getInstance().getUser();
        String token = user != null ? user.getToken() : null;
        if (!TextUtils.isEmpty(token)) {
            headers.put("Authorization", token);
        }
        return headers;
    }

    @Override
    public HashMap<String, Object> getParams(String method, HashMap<String, Object> params) {
        if(params == null) {
            return null;
        }
        HashMap<String, Object> allParams;
        if (params != null) {
            allParams = params;
        } else {
            allParams = new HashMap<>();
        }
        //allParams.put("versionCode", VERSIONCODE);
        //allParams.put("versionName", VERSIONNAME);
        //allParams.put("method", StringUtil.string(method));
        allParams.put("timestamp", DateTimeUtil.getServerCurrentTimeMillis() + "");
        //allParams.put("version", "1");
        return allParams;
    }

    public String getIMEI() {
        return IMEI;
    }

    public String getIMSI() {
        return IMSI;
    }

    public String getVERSIONCODE() {
        return VERSIONCODE;
    }

    public String getVERSIONNAME() {
        return VERSIONNAME;
    }

    public String getCHANNELID() {
        return CHANNELID;
    }

}
